<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
    <title>订单管理</title>
    <c:set var="path" value="${pageContext.request.contextPath}"></c:set>
    <link rel="stylesheet" href="${path}/layui/css/layui.css">
    <link rel="stylesheet" href="/layui/css/formSelects-v4.css" media="all ">
    <script src="${path}/layui/layui.js"></script>
    <script src="${path}/js/jquery.min.js"></script>
    <style>

    </style>
</head>

<body>
    <h2>订单信息列表</h2>
    <hr>
        <form class="layui-form" action="">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名:</label>
                <div class="layui-input-inline" style="width: 200px">
                    <input id="custName" type="text" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-input-inline" style="width: 200px">
                    <button type="button" class="layui-btn" onclick="SearchData();"><i class="layui-icon layui-icon-search"></i>查询</button>
                </div>
            </div>
        </div>
    </form>
    <div class="layui-fluid">
        <table id="orderTable" lay-filter="orderTable"></table>
    </div>


    <script>
        layui.use(["table","form"],function () {
            var table=layui.table;
            //渲染表格
            table.render({
                elem:"#orderTable",
                url:"${path}/customers/list",
                page:true,
                cols:[[
                    {field:'id',title:'ID'},
                    {field:'custName',title:'客户名称'},
                    {templet:'#dengji',title:'客户类型'},
                    {templet:'#stat',title:'状态'},
                    {toolbar:'#barDemo',title:'操作'}

                ]]
            })
            //监听工具栏事件
            table.on('tool(orderTable)',function (obj) {
                var data=obj.data;
                var e=obj.event;
                switch (e) {
                    case "xiadan":
                        console.log(data)
                        $.get("${path}/order/xiadan",function (str) {
                            layer.open({
                                type:1,
                                title:'下单操作',
                                area:'700px',
                                offset:"260px",
                                content:str,
                                skin:'layui-layer-molv',
                                btn:['确定','取消'],
                                success:function(){
                                    $("#a").val(data.custName);
                                    $("#b").val(data.dictionarieslevel.name);
                                    $("#c").val(data.dictionariesstat.name);
                                    $("#d").val(data.phone);
                                    $("#khid1").val(data.id);


                                },
                                yes:function () {
                                    var rows=table.checkStatus("shoppTable");
                                    var goumai=rows.data;
                                    if (goumai.length==0){
                                        layer.msg("请选择要购买的商品",function () {});
                                         return;
                                    }
                                    var params="";
                                    var idss=[];
                                    var id="";
                                    var shuliang="";
                                    for (let CrmShopping of goumai){
                                        params+="ids="+CrmShopping.id+"&"+"xznum="+CrmShopping.num+"&";
                                        id+=CrmShopping.id+",";
                                        shuliang+=CrmShopping.num+",";
                                        idss+=CrmShopping.num+",";

                                    }
                                    var slll="";
                                    for(let orderdetailss of goumai){
                                        if(orderdetailss.num == undefined || orderdetailss.num == " "|| orderdetailss.num == 0){
                                            slll++;
                                        }
                                    }
                                    if(slll>0){
                                        layer.msg("有商品数量为空或者为0不可添加...",function () {
                                            
                                        });

                                    }else{
                                        $.post("${path}/order/updatefor",{
                                            'ids':id,
                                            'shuliang':shuliang
                                        },function () {
                                            table.reload("qingdanTable");
                                            $.post("${path}/order/jine",{'ids':id},function (date) {
                                                console.log(date);
                                            })
                                        })

                                        $.post("${path}/order/jiesuan",params,function (str) {
                                                layer.open({
                                                    type:1,
                                                    title:'填写并核对订单信息',
                                                    area:'700px',
                                                    offset:'260px',
                                                    content:str,
                                                    skin:'layui-layer-molv',
                                                    btn:['结算订单','取消'],
                                                    success:function(){
                                                        var datas = rows.data;
                                                        console.log(datas)
                                                        $("#name").val(data.custName);
                                                        $("#phone").val(data.phone);
                                                        $("#address").val(data.address);
                                                        $("#khid").val($("#khid1").val());
                                                        $("#kehubianhao").val(data.custName);
                                                        for(let aa of datas){

                                                        }
                                                        $("#xznum").val(1);
                                                        // console.log("cbdhbc"+$("#xznum").val(1));

                                                    },
                                                    yes:function (index) {
                                                        //存储订单表
                                                        var str=$("#distriFormssaa").serialize();
                                                        console.log(str)
                                                        $.post("${path}/order/add",str,function () {
                                                            var rows=table.checkStatus("qingdanTable");
                                                            var goumai=rows.data;
                                                            if (goumai.length==0){
                                                                layer.msg("请选择要购买的商品",function () {});
                                                                return;
                                                            }
                                                            var params = "";
                                                            var i = 0;
                                                            var ii = 0;
                                                            var iii = 0;
                                                            var iiii=0
                                                            for(let orderdetails of goumai){
                                                                params += "list["+(i++)+"].shoppingid="+orderdetails.id+"&"+"list["+(ii++)+"].name="+orderdetails.name+"&"+"list["+(iii++)+"].fuzhu="+orderdetails.fuzhu+"&"+"list["+(iiii++)+"].price="+orderdetails.price+"&";
                                                            }
                                                            //?指客户id
                                                            params += "ordered="+data.custName;


                                                                $.post("${path}/order/bathadd",params,function () {
                                                                    layer.msg("恭喜您购买成功...",{time:1000},function () {
                                                                        layer.closeAll();
                                                                    })
                                                                })


                                                        })



                                                        console.log(params)
                                                    }
                                                })
                                            })
                                    }
                                }
                            })
                        })
                }
            })
        })

        function SearchData() {
            layui.table.reload("orderTable",{
                page:{
                    curr:1
                },
                where:{
                    "custName":$("#custName").val()
                }
            })
        }

    </script>
    <script type="text/html" id="dengji">
        {{d.dictionarieslevel.name}}
    </script>
    <script type="text/html" id="stat">
        {{d.dictionariesstat.name}}
    </script>
    <script type="text/html" id="fenpeistat">
        {{d.dictionariesfenpeistat.name}}
    </script>
    <script type="text/html" id="people">
        {{d.crmUserpeople.username}}
    </script>

    <script type="text/html" id="barDemo">
        <a href="javascript:void (0)" lay-event="xiadan"><button class="layui-btn">下单</button></a>
    </script>
</body>
</html>
